package com.gxa.dao.impl;

import com.gxa.common.Constants;
import com.gxa.common.ConstantsService;
import com.gxa.dao.ServiceDao;
import com.gxa.dbutils.JdbcBuild;
import com.gxa.pojo.Problem;
import com.gxa.pojo.ServiceManagementInfo;

import java.sql.SQLException;
import java.util.List;

public class ServiceDaoImp extends JdbcBuild implements ServiceDao {

    @Override
    public List<ServiceManagementInfo> queryServiceManagementProcessingList(Integer param01, Integer param02,String serviceName ,String type) throws SQLException {
        List<ServiceManagementInfo> ServiceManagerList =null;
        String sql = "select * from service_management where 1=1 and service_status = ? and status=?";
        if (serviceName==null && type==null){
            sql = sql+" limit ?,?";
            Object[] params = {ConstantsService.STATUS_ALLOCATION,Constants.STATUS_OK,param01,param02};
            ServiceManagerList = queryListEntity(ServiceManagementInfo.class, sql, params);
//            System.out.println(sql);
//            System.out.println(Arrays.toString(params));
        }
        if (serviceName!=null && type==null){
            sql = sql+" and service_name like ? limit ?,?";
            Object[] params = {ConstantsService.STATUS_ALLOCATION,Constants.STATUS_OK,"%"+serviceName+"%",param01,param02};
            ServiceManagerList = queryListEntity(ServiceManagementInfo.class, sql, params);
        }
        if(type!=null && serviceName==null){
            sql = sql +" and service_type = ? limit ?,?";
            Object[] params ={ConstantsService.STATUS_ALLOCATION,Constants.STATUS_OK,type,param01,param02};
            ServiceManagerList = queryListEntity(ServiceManagementInfo.class, sql, params);
        }
        if (type!=null && serviceName!=null){
            sql = sql +" and service_name like ? and service_type = ? limit ?,?";
            Object[] params ={ConstantsService.STATUS_ALLOCATION,Constants.STATUS_OK,"%"+serviceName+"%",type,param01,param02};
            ServiceManagerList = queryListEntity(ServiceManagementInfo.class, sql, params);
        }
        return ServiceManagerList;
    }

    @Override
    public Integer countServiceProcessingInfo(String serviceName ,String type) throws SQLException {
        int allForInt =0;
        String sql = "select count(*) from service_management where 1=1 and service_status = ? and status=?";
        if (serviceName==null && type==null){
            Object[] params = {ConstantsService.STATUS_ALLOCATION,Constants.STATUS_OK};
            allForInt = findAllForInt(sql, params);
        }
        if (serviceName!=null && type==null){
            sql = sql+" and service_name like ?";
            Object[] params = {ConstantsService.STATUS_ALLOCATION,Constants.STATUS_OK,"%"+serviceName+"%"};
            allForInt = findAllForInt(sql, params);
        }
        if(type!=null && serviceName==null){
            sql = sql +" and service_type = ?";
            Object[] params = {ConstantsService.STATUS_ALLOCATION,Constants.STATUS_OK,type};
            allForInt = findAllForInt(sql, params);
        }
        if (serviceName!=null && type!=null){
            sql = sql +" and service_name like ? and service_type = ?";
            Object[] params = {ConstantsService.STATUS_ALLOCATION,Constants.STATUS_OK,"%"+serviceName+"%",type};
            allForInt = findAllForInt(sql, params);
        }
        return allForInt;
    }

    @Override
    public List<ServiceManagementInfo> queryServiceManagementFeedbackList(Integer param01, Integer param02, String serviceName, String type) throws SQLException {
        List<ServiceManagementInfo> ServiceManagerList =null;
        String sql = "select * from service_management where 1=1 and service_status = ? and status=?";
        if (serviceName==null && type==null){
            sql = sql+" limit ?,?";
            Object[] params = {ConstantsService.STATUS_HANDLE,Constants.STATUS_OK,param01,param02};
            ServiceManagerList = queryListEntity(ServiceManagementInfo.class, sql, params);
//            System.out.println(sql);
//            System.out.println(Arrays.toString(params));
        }
        if (serviceName!=null && type==null){
            sql = sql+" and service_name like ? limit ?,?";
            Object[] params = {ConstantsService.STATUS_HANDLE,Constants.STATUS_OK,"%"+serviceName+"%",param01,param02};
            ServiceManagerList = queryListEntity(ServiceManagementInfo.class, sql, params);
        }
        if(type!=null && serviceName==null){
            sql = sql +" and service_type = ? limit ?,?";
            Object[] params ={ConstantsService.STATUS_HANDLE,Constants.STATUS_OK,type,param01,param02};
            ServiceManagerList = queryListEntity(ServiceManagementInfo.class, sql, params);
        }
        if (type!=null && serviceName!=null){
            sql = sql +" and service_name like ? and service_type = ? limit ?,?";
            Object[] params ={ConstantsService.STATUS_HANDLE,Constants.STATUS_OK,"%"+serviceName+"%",type,param01,param02};
            ServiceManagerList = queryListEntity(ServiceManagementInfo.class, sql, params);
        }
        return ServiceManagerList;
    }

    @Override
    public Integer countServiceFeedbackInfo(String serviceName, String type) throws SQLException {
        int allForInt =0;
        String sql = "select count(*) from service_management where 1=1 and service_status = ? and status=?";
        if (serviceName==null && type==null){
            Object[] params = {ConstantsService.STATUS_HANDLE,Constants.STATUS_OK};
            allForInt = findAllForInt(sql, params);
        }
        if (serviceName!=null && type==null){
            sql = sql+" and service_name like ?";
            Object[] params = {ConstantsService.STATUS_HANDLE,Constants.STATUS_OK,"%"+serviceName+"%"};
            allForInt = findAllForInt(sql, params);
        }
        if(type!=null && serviceName==null){
            sql = sql +" and service_type = ?";
            Object[] params = {ConstantsService.STATUS_HANDLE,Constants.STATUS_OK,type};
            allForInt = findAllForInt(sql, params);
        }
        if (serviceName!=null && type!=null){
            sql = sql +" and service_name like ? and service_type = ?";
            Object[] params = {ConstantsService.STATUS_HANDLE,Constants.STATUS_OK,"%"+serviceName+"%",type};
            allForInt = findAllForInt(sql, params);
        }
        return allForInt;
    }

    @Override
    public List<ServiceManagementInfo> queryServiceManagementArchivingList(Integer param01, Integer param02, String serviceName, String type,Integer service_status) throws SQLException {
        List<ServiceManagementInfo> ServiceManagerList =null;
        String sql = "select * from service_management where 1=1 and (service_status = ? or service_status = ?) and status=?";
        if (serviceName==null && type==null && service_status==0){
            sql = sql+" limit ?,?";
            Object[] params = {ConstantsService.STATUS_FEEDBACK,ConstantsService.STATUS_FILE,Constants.STATUS_OK,param01,param02};
            ServiceManagerList = queryListEntity(ServiceManagementInfo.class, sql, params);
//            System.out.println(sql);
//            System.out.println(Arrays.toString(params));
        }
        if (serviceName!=null && type==null && service_status==0){
            sql = sql+" and service_name like ? limit ?,?";
            Object[] params = {ConstantsService.STATUS_FEEDBACK,ConstantsService.STATUS_FILE,Constants.STATUS_OK,"%"+serviceName+"%",param01,param02};
            ServiceManagerList = queryListEntity(ServiceManagementInfo.class, sql, params);
        }
        if(type!=null && serviceName==null && service_status==0){
            sql = sql +" and service_type = ? limit ?,?";
            Object[] params ={ConstantsService.STATUS_FEEDBACK,ConstantsService.STATUS_FILE,Constants.STATUS_OK,type,param01,param02};
            ServiceManagerList = queryListEntity(ServiceManagementInfo.class, sql, params);
        }
        if (type!=null && serviceName!=null && service_status==0){
            sql = sql +" and service_name like ? and service_type = ? limit ?,?";
            Object[] params ={ConstantsService.STATUS_FEEDBACK,ConstantsService.STATUS_FILE,Constants.STATUS_OK,"%"+serviceName+"%",type,param01,param02};
            ServiceManagerList = queryListEntity(ServiceManagementInfo.class, sql, params);
        }
        if (serviceName==null && type==null && service_status!=0){
            sql = sql+" and service_status = ? limit ?,?";
            Object[] params = {ConstantsService.STATUS_FEEDBACK,ConstantsService.STATUS_FILE,Constants.STATUS_OK,service_status,param01,param02};
            ServiceManagerList = queryListEntity(ServiceManagementInfo.class, sql, params);
//            System.out.println(sql);
//            System.out.println(Arrays.toString(params));
        }
        if (serviceName!=null && type==null && service_status!=0){
            sql = sql+" and service_name like ? and service_status = ? limit ?,?";
            Object[] params = {ConstantsService.STATUS_FEEDBACK,ConstantsService.STATUS_FILE,Constants.STATUS_OK,"%"+serviceName+"%",service_status,param01,param02};
            ServiceManagerList = queryListEntity(ServiceManagementInfo.class, sql, params);
        }
        if(type!=null && serviceName==null && service_status!=0){
            sql = sql +" and service_type = ? and service_status = ? limit ?,?";
            Object[] params ={ConstantsService.STATUS_FEEDBACK,ConstantsService.STATUS_FILE,Constants.STATUS_OK,type,service_status,param01,param02};
            ServiceManagerList = queryListEntity(ServiceManagementInfo.class, sql, params);
        }
        if (type!=null && serviceName!=null && service_status!=0){
            sql = sql +" and service_name like ? and service_type = ? and service_status = ? limit ?,?";
            Object[] params ={ConstantsService.STATUS_FEEDBACK,ConstantsService.STATUS_FILE,Constants.STATUS_OK,"%"+serviceName+"%",type,service_status,param01,param02};
            ServiceManagerList = queryListEntity(ServiceManagementInfo.class, sql, params);
        }
        return ServiceManagerList;
    }

    @Override
    public Integer countServiceArchivingInfo(String serviceName, String type,Integer service_status) throws SQLException {
        int allForInt =0;
        String sql = "select count(*) from service_management where 1=1 and (service_status = ? or service_status = ?) and status=?";
        if (serviceName==null && type==null && service_status==0){
            Object[] params = {ConstantsService.STATUS_FEEDBACK,ConstantsService.STATUS_FILE,Constants.STATUS_OK};
            allForInt = findAllForInt(sql, params);
        }
        if (serviceName!=null && type==null && service_status==0){
            sql = sql+" and service_name like ?";
            Object[] params = {ConstantsService.STATUS_FEEDBACK,ConstantsService.STATUS_FILE,Constants.STATUS_OK,"%"+serviceName+"%"};
            allForInt = findAllForInt(sql, params);
        }
        if(type!=null && serviceName==null && service_status==0){
            sql = sql +" and service_type = ?";
            Object[] params = {ConstantsService.STATUS_FEEDBACK,ConstantsService.STATUS_FILE,Constants.STATUS_OK,type};
            allForInt = findAllForInt(sql, params);
        }
        if (serviceName!=null && type!=null && service_status==0){
            sql = sql +" and service_name like ? and service_type = ?";
            Object[] params = {ConstantsService.STATUS_FEEDBACK,ConstantsService.STATUS_FILE,Constants.STATUS_OK,"%"+serviceName+"%",type};
            allForInt = findAllForInt(sql, params);
        }
        if (serviceName==null && type==null && service_status!=0){
            sql = sql+" and service_status = ?";
            Object[] params = {ConstantsService.STATUS_FEEDBACK,ConstantsService.STATUS_FILE,Constants.STATUS_OK,service_status};
            allForInt = findAllForInt(sql, params);
        }
        if (serviceName!=null && type==null && service_status!=0){
            sql = sql+" and service_name like ? and service_status = ?";
            Object[] params = {ConstantsService.STATUS_FEEDBACK,ConstantsService.STATUS_FILE,Constants.STATUS_OK,"%"+serviceName+"%",service_status};
            allForInt = findAllForInt(sql, params);
        }
        if(type!=null && serviceName==null && service_status!=0){
            sql = sql +" and service_type = ? and service_status = ?";
            Object[] params = {ConstantsService.STATUS_FEEDBACK,ConstantsService.STATUS_FILE,Constants.STATUS_OK,type,service_status};
            allForInt = findAllForInt(sql, params);
        }
        if (serviceName!=null && type!=null && service_status!=0){
            sql = sql +" and service_name like ? and service_type = ? and service_status = ?";
            Object[] params = {ConstantsService.STATUS_FEEDBACK,ConstantsService.STATUS_FILE,Constants.STATUS_OK,"%"+serviceName+"%",type,service_status};
            allForInt = findAllForInt(sql, params);
        }
        return allForInt;
    }

    @Override
    public List<Problem> queryServiceManagementManagementList(Integer param01, Integer param02, String ptitle, String ptype) throws SQLException {
        List<Problem> problems =null;
        String sql = "select * from problem where 1=1  and pstatus=?";
        if (ptitle==null && ptype==null){
            sql = sql+" limit ?,?";
            Object[] params = {Constants.STATUS_OK,param01,param02};
            problems = queryListEntity(Problem.class, sql, params);
//            System.out.println(sql);
//            System.out.println(Arrays.toString(params));
        }
        if (ptitle!=null && ptype==null){
            sql = sql+" and ptitle like ? limit ?,?";
            Object[] params = {Constants.STATUS_OK,"%"+ptitle+"%",param01,param02};
            problems = queryListEntity(Problem.class, sql, params);
        }
        if(ptype!=null && ptitle==null){
            sql = sql +" and ptype = ? limit ?,?";
            Object[] params ={Constants.STATUS_OK,ptype,param01,param02};
            problems = queryListEntity(Problem.class, sql, params);
        }
        if (ptype!=null && ptitle!=null){
            sql = sql +" and ptitle like ? and ptype = ? limit ?,?";
            Object[] params ={Constants.STATUS_OK,"%"+ptitle+"%",ptype,param01,param02};
            problems = queryListEntity(Problem.class, sql, params);
        }
        return problems;
    }

    @Override
    public Integer countServiceManagementInfo(String ptitle, String ptype) throws SQLException {
        int allForInt =0;
        String sql = "select count(*) from problem where 1=1  and pstatus=?";
        if (ptitle==null && ptype==null){
            Object[] params = {Constants.STATUS_OK};
            allForInt = findAllForInt(sql, params);
        }
        if (ptitle!=null && ptype==null){
            sql = sql+" and ptitle like ?";
            Object[] params = {Constants.STATUS_OK,"%"+ptitle+"%"};
            allForInt = findAllForInt(sql, params);
        }
        if(ptype!=null && ptitle==null){
            sql = sql +" and ptype = ?";
            Object[] params = {Constants.STATUS_OK,ptype};
            allForInt = findAllForInt(sql, params);
        }
        if (ptitle!=null && ptype!=null){
            sql = sql +" and ptitle like ? and ptype = ?";
            Object[] params = {Constants.STATUS_OK,"%"+ptitle+"%",ptype};
            allForInt = findAllForInt(sql, params);
        }
        return allForInt;
    }

    @Override
    public int delServiceById(String id) throws SQLException {
        String sql = "update service_management set status=? where id=?";
        Object[] params = {Constants.STATUS_DEL,id};
        int result = super.executeUpdate(sql, params);
        return result;
    }

    @Override
    public int delManagementServiceById(String id) throws SQLException {
        String sql = "update problem set pstatus=? where id=?";
        Object[] params = {Constants.STATUS_DEL,id};
        int result = super.executeUpdate(sql, params);
        return result;
    }

    @Override
    public int updateServiceProcessingById(ServiceManagementInfo sm) throws SQLException {
        String sql = "update service_management set service_status=?,note_information=?,last_operation_time=? where id = ?";
        Object[] params = {ConstantsService.STATUS_HANDLE,sm.getNote_information(),sm.getLast_operation_time(),sm.getId()};
        int result = super.executeUpdate(sql, params);
        return result;
    }

    @Override
    public int updateServiceFeedbackById(ServiceManagementInfo sm) throws SQLException {
        String sql = "update service_management set service_status=?,service_feedback=?,last_operation_time=? where id = ?";
        Object[] params = {ConstantsService.STATUS_FEEDBACK,sm.getService_feedback(),sm.getLast_operation_time(),sm.getId()};
        int result = super.executeUpdate(sql, params);
        return result;
    }

    @Override
    public int updateServiceArchivingById(ServiceManagementInfo sm) throws SQLException {
        String sql = "update service_management set service_status=?,last_operation_time=? where id = ?";
        Object[] params = {ConstantsService.STATUS_FILE,sm.getLast_operation_time(),sm.getId()};
        int result = super.executeUpdate(sql, params);
        return result;
    }

    @Override
    public int updateServiceManagementById(Problem p) throws SQLException {
        String sql = "update problem set ptitle=?,ptype=?,pauthor=?,plast_time=?,pmethod=?,pnotes=? where id = ?";
        Object[] params = {p.getPtitle(),p.getPtype(),p.getPauthor(),p.getPlast_time(),p.getPmethod(),p.getPnotes(),p.getId()};
        int result = super.executeUpdate(sql, params);
        return result;
    }

    @Override
    public int addServiceManagementById(Problem p) throws SQLException {
        String sql = "insert into problem(ptitle,ptype,pauthor,plast_time,pmethod,pnotes) values(?,?,?,?,?,?)";
        Object[] params = {p.getPtitle(),p.getPtype(),p.getPauthor(),p.getPlast_time(),p.getPmethod(),p.getPnotes()};
        int result = super.executeUpdate(sql, params);
        return result;
    }
}
